package beeboxes;

import eightDataType.ByteUtil;
import eightDataType.mStr.StringToHex16;
import utils.PrintUtil;

/**
 * @author Ajie
 * @date 2022/10/18
 * @function
 */
public class HIDCardDecode {
    private static final String uid = "d2d73f5e";// kbw
    private static final String pacs = "02020007";// kbw
    //ATR is: 3B8F8001804F0CA0000003060300020000000069 ,UID is: D2D73F5E ,PACS is: 808001C0


    private static final String pacs_d = "02020007";// kbw

    // TODO: 2022/10/20 记得补0
    private static final String toSendFinal = "123456";

    public static void main(String[] args) {
        try {
            String ori = "3537321822＿222＿1＿333＿3";
//            String ori = "AEF_bde_123";
            if (ori.contains("＿")){
                ori = ori.replaceAll("＿","_");
            }

            boolean valide = checkCardInputIsDEC(ori);
            if (!valide){
                System.err.println("HIDCardDecode:main:getType: CardNo is not Dec. need set 5127 readCard mode set KBW mode is DEC output. 需要设置 5127 模组 KBW 模式下 PACS 和 CSN 输出为 DEC. ");
            }

            System.out.println("HIDCardDecode:main::::"+("__".charAt(0)=='_'));

            byte[] cardByteArray = new byte[1024];
//            String hexStr_1 = "01130300A91EE602";
            String hexStr_1 = "1130300A91EE602";
            String decStr_1 = "77408919967688194";
//            int strToInt = Integer.parseInt(decStr_1);//太长了
//            String s3 = Integer.toHexString(strToInt);
//            long longData = Long.parseLong(hexStr_1);//NumberFormatExceptio

            long maxValue = Long.MAX_VALUE;
            long longData = Long.parseLong(decStr_1);
            String format = StringToHex16.numToHex32(longData);//
            System.out.println("HIDCardDecode:main:long:" + longData + "   ---> " + format);
            System.out.println("HIDCardDecode:main:Long.MAX_VALUE:" + Long.MAX_VALUE);


            System.out.println("HIDCardDecode:main:s3::" + format.equalsIgnoreCase(hexStr_1));
            String s1 = StringToHex16.hexToDec(hexStr_1);
            System.out.println("HIDCardDecode:main:hexStr2Str:" + s1 + " " + (s1.equals(decStr_1)));

            String s2 = StringToHex16.str2HexStr(decStr_1);
            System.out.println("HIDCardDecode:main:s2:" + s2 + " " + (s2.equals(hexStr_1)));


            byte[] aeees = toBytes("AABBCC");//-86-69-52
//            byte[] aeees = ByteUtil.hexToByteArray("_AABBCC");//-86-69-52 //NumberFormatException
            PrintUtil.print(aeees);
            for (int i = 0; i < aeees.length; i++) {
                cardByteArray[i] = aeees[i];
            }


            String s = ByteUtil.bytesToHex(aeees);
            System.out.println("HIDCardDecode:main:byte-->HEX:" + s);
            M1Card m1Card = new M1Card(cardByteArray, aeees.length);
            System.err.println("Send a m1Card signal, >>>>getUid=" + TextsUtils.bytesToHexString(m1Card.getUid(), 0, m1Card.getUid().length));

        } catch (Exception e) {
            System.out.println("HIDCardDecode:main: str-->hex exception...");
            e.printStackTrace();
        }
//        originTest();

        System.out.println("HIDCardDecode:main:正常");

    }

    /**
     * 将16进制字符串转换为byte[]
     *
     * @param str
     * @return
     */
    public static byte[] toBytes(String str) {
        if (str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }

    private static void originTest() {
        byte[] cardByteArray = new byte[1024];
        for (int i = 0; i < toSendFinal.length() / 2; i++) {
            int index = i * 2;
            int val = Integer.parseInt(toSendFinal.substring(index, index + 2), 16);
            cardByteArray[i] = (byte) val;
        }
        M1Card m1Card = new M1Card(cardByteArray, toSendFinal.length());
        System.out.println("Send a m1Card signal, getUid=" + TextsUtils.bytesToHexString(m1Card.getUid(), 0, m1Card.getUid().length));

    }

    //检查是否10进制。
    public static boolean checkCardInputIsDEC(String data){
        if (data == null) return false;
        for (int i = 0; i < data.length(); i++) {
            if (data.charAt(i) == '_'){
                continue;
            }
            if (data.charAt(i)>'9' || data.charAt(i)<'0'){
                return false;
            }
        }

        return true;
    }
}
